import unittest
import app
import utils
from parameterized import parameterized
from api.iHRM import LoginAPI, EmpManageApi


class TestIHRMEmpManageParams(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = LoginAPI()
        cls.manage_api = EmpManageApi()

    @parameterized.expand(utils.get_json_data(app.BASE_PATH + '/data/curd.json'))
    def test_emp_curd(self, login_data, add_data, modify, status_code, success, code, message):
        # 登录获取令牌
        resp = self.login_api.login(login_data)
        print(resp.json())
        # 获取登录成功的令牌，写入全局变量
        app.TOKEN = 'Bearer ' + resp.json().get('data')
        print(app.TOKEN)

        # 添加员工
        # 在app原有的HEADERS上添加一组新的kv    'Authorization':'BEARER xxxx'
        app.HEADERS['Authorization'] = app.TOKEN
        resp_add_emp = self.manage_api.add_emp(app.HEADERS, add_data)
        utils.common_assert(self, resp_add_emp, status_code, success, code, message)

        # 查询员工
        resp_query_emp = self.manage_api.query_emp(app.HEADERS)
        utils.common_assert(self, resp_query_emp, status_code, success, code, message)

        # 修改员工
        resp_modify_emp = self.manage_api.modify_emp(app.HEADERS, modify)
        utils.common_assert(self, resp_modify_emp, status_code, success, code, message)

        # 再查询
        resp_query_emp_again = self.manage_api.query_emp(app.HEADERS)
        utils.common_assert(self, resp_query_emp_again, status_code, success, code, message)

        # 删除员工
        resp_del_emp = self.manage_api.delete_emp(app.HEADERS)
        utils.common_assert(self, resp_del_emp, status_code, success, code, message)

        # 查询员工列表
        resp_query_emp_list = self.manage_api.query_emp_list(app.HEADERS)
        utils.common_assert(self, resp_query_emp_list, status_code, success, code, message)
